<?php
class ChannelProductModel
{
    /*
        添加商品信息
        params
            channel_id 渠道id
            store_id 店铺id
            product_id 商品id
            other_category_id 第三方分类id
            other_product_id 第三方商品id
            product_price 商品销售价格 单位：分
            other_spec_id 商品规格ID
    */
    public function addChannelProductInfo($channel_id = 0, $store_id = 0, $product_id = 0, $other_category_id = '', $other_product_id = '', $product_price = 0, $other_spec_id = 0)
    {
        $Dao = M("kfsy_channel_store.kf_channel_product");

        $data = array();
        $data['fk_channel_id'] = $channel_id;
        $data['fk_store_id'] = $store_id;
        $data['fk_product_id'] = $product_id;
        $data['other_category_id'] = $other_category_id;
        $data['other_product_id'] = $other_product_id;
        $data['product_price'] = $product_price;
        $data['other_spec_id'] = $other_spec_id;
        $data['product_stock'] = 0;
        $data['product_status'] = 1;
        $data['createtime'] = date("Y-m-d H:i:s");

        $add = $Dao->add($data);

        if($add == false){
            DLOG("添加商品信息失败 sql: ". $Dao->getLastSql(), "error", "channel_db");

            return false;
        }

        return $add;
    }

    /*
        替换商品信息
        params
            channel_id 渠道id
            store_id 店铺id
            product_id 商品id
            other_category_id 第三方分类id
            other_product_id 第三方商品id
            product_price 商品销售价格 单位：分
            product_stock 商品库存
            product_status 商品状态 1-上架 2-下架
    */
    public function replacChannelProductInfo($channel_id = 0, $store_id = 0, $product_id = 0, $other_category_id = '', $other_product_id = '', $product_price = 0, $product_stock = 0, $product_status = 0)
    {
        $Dao = M("kfsy_channel_store.kf_channel_product");

        $data = array();
        $data['fk_channel_id'] = $channel_id;
        $data['fk_store_id'] = $store_id;
        $data['fk_product_id'] = $product_id;
        $data['other_category_id'] = $other_category_id;
        $data['other_product_id'] = $other_product_id;
        $data['product_price'] = $product_price;
        $data['product_stock'] = $product_stock;
        $data['product_status'] = $product_status;
        $data['createtime'] = date("Y-m-d H:i:s");

        $add = $Dao->add($data, true);

        if($add == false){
            DLOG("替换商品信息失败 sql: ". $Dao->getLastSql(), "error", "channel_db");

            return false;
        }

        return $add;
    }

    /*
        添加图片信息
        params
            channel_id 渠道id
            store_id 店铺id
            product_id 商品id
            product_thumb 图片地址数组对象
    */
    public function addChannelProductThumb($channel_id = 0, $store_id = 0, $product_id = 0, $product_thumb = array())
    {
        $Dao = M("kfsy_channel_store.kf_channel_product_thumb");

        $time = date("Y-m-d H:i:s");

        $data = array();

        foreach ($product_thumb as $key => $value) {
            $data[] = array(
                "fk_channel_id" => $channel_id,
                "fk_store_id" => $store_id,
                "fk_product_id" => $product_id,
                "thumb_href" => $value,
                "createtime" => $time,
            );
        }

        if(empty($data)){
            return true;
        }
        
        $add = $Dao->addAll($data);

        if($add == false){
            DLOG("创建商品图片信息失败 sql: " . $Dao->getLastSql(), "error", "channel_db");
            return false;
        }

        return true;
    }

    /*
        删除第三方商品
        params
            channel_id 渠道id
            store_id 店铺id
            product_id 快方商品id
    */
    public function clearChannelProduct($channel_id = 0, $store_id = 0, $product_id = 0)
    {
        $Dao = M("kfsy_channel_store.kf_channel_product");

        $where = array();
        $where[] = sprintf("fk_channel_id = %s", $channel_id);
        $where[] = sprintf("fk_store_id = %s", $store_id);
        $where[] = sprintf("fk_product_id = %s", $product_id);

        $where = implode(" AND ", $where);
        $res = $Dao->where($where)->delete();

        return $res;
    }

    /*
        清空第三方商品图片
        params
            channel_id 渠道id
            store_id 店铺id
            product_id 快方商品id
    */
    public function clearChannelProductThumb($channel_id = 0, $store_id = 0, $product_id = 0)
    {
        $Dao = M("kfsy_channel_store.kf_channel_product_thumb");

        $where = array();
        $where[] = sprintf("fk_channel_id = %s", $channel_id);
        $where[] = sprintf("fk_store_id = %s", $store_id);
        $where[] = sprintf("fk_product_id = %s", $product_id);

        $where = implode(" AND ", $where);
        $Dao->where($where)->delete();
    }

    /*
        获取第三方商品信息
        params
            channel_id 渠道id
            store_id 店铺id
            product_id 快方商品id
    */
    public function getChannelProductInfo($channel_id = 0, $store_id = 0, $product_id = 0)
    {
        $Dao = M("kfsy_channel_store.kf_channel_product");

        $where = array();
        $where[] = sprintf("fk_channel_id = %s", $channel_id);
        $where[] = sprintf("fk_store_id = %s", $store_id);
        $where[] = sprintf("fk_product_id = %s", $product_id);

        $where = implode(" AND ", $where);


        $productInfo = $Dao
            ->where($where)
            ->find();

        return $productInfo;
    }

    /*
        获取第三方商品规格ID
        params
            channel_id 渠道id
            store_id 店铺id
            other_product_id 第三方商品id
    */
    public function getChannelProductSpecID($channel_id = 0, $store_id = 0, $other_product_id = 0)
    {
        $Dao = M("kfsy_channel_store.kf_channel_product");

        $where = array();
        $where[] = sprintf("fk_channel_id = %s", $channel_id);
        $where[] = sprintf("fk_store_id = %s", $store_id);
        $where[] = sprintf("other_product_id = %s", $other_product_id);

        $where = implode(" AND ", $where);

        $res = $Dao
            ->where($where)
            ->find();

        return $res;
    }

    /*
        更新第三方商品信息
        params
            channel_id 渠道id
            store_id 店铺id
            product_id 快方商品id
            data  更新的数据对象
    */
    public function updateChannelProductInfo($channel_id = 0, $store_id = 0, $product_id = 0, $data = array())
    {
        $Dao = M("kfsy_channel_store.kf_channel_product");

        $where = array();
        $where[] = sprintf("fk_channel_id = %s", $channel_id);
        $where[] = sprintf("fk_store_id = %s", $store_id);
        $where[] = sprintf("fk_product_id = %s", $product_id);

        $where = implode(" AND ", $where);

        if(empty($data)){
            return true;
        }

        $productInfo = $Dao
            ->where($where)
            ->save($data);

        return $productInfo;
    }

    /*
        更新第三方商品库存
        params
            channel_id 渠道id
            store_id 店铺id
            other_product_id 第三方商品id
            data  更新的数据对象
    */
    public function updateChannelProductStock($channel_id = 0, $store_id = 0, $other_product_id = 0, $data = array())
    {
        $Dao = M("kfsy_channel_store.kf_channel_product");

        $where = array();
        $where[] = sprintf("fk_channel_id = %s", $channel_id);
        $where[] = sprintf("fk_store_id = %s", $store_id);
        $where[] = sprintf("other_product_id = %s", $other_product_id);

        $where = implode(" AND ", $where);

        if(empty($data)){
            return true;
        }

        $productInfo = $Dao
            ->where($where)
            ->save($data);

        return $productInfo;
    }

    /*
        更新第三方商品价格
        params
            channel_id 渠道id
            store_id 店铺id
            other_product_id 第三方商品id
            data  更新的数据对象
    */
    public function updateChannelProductPrice($channel_id = 0, $store_id = 0, $other_product_id = 0, $data = array())
    {
        $Dao = M("kfsy_channel_store.kf_channel_product");

        $where = array();
        $where[] = sprintf("fk_channel_id = %s", $channel_id);
        $where[] = sprintf("fk_store_id = %s", $store_id);
        $where[] = sprintf("other_product_id = %s", $other_product_id);

        $where = implode(" AND ", $where);

        if(empty($data)){
            return true;
        }

        $productInfo = $Dao
            ->where($where)
            ->save($data);

        return $productInfo;
    }

    /*
        获取第三方商品库存信息
        params
            channel_id 渠道id
            store_id 店铺id
    */
    public function getChannelProductStock($channel_id = 0, $store_id = 0)
    {
        $Dao = M("kfsy_channel_store.kf_channel_product");

        $where = array();
        $where[] = sprintf("fk_channel_id = %s", $channel_id);
        $where[] = sprintf("fk_store_id = %s", $store_id);

        $where = implode(" AND ", $where);

        $stockInfo = $Dao
            ->field("other_product_id, product_stock")
            ->where($where)
            ->select();

        return $stockInfo;
    }

    /*
        获取第三方商品价格信息
        params
            channel_id 渠道id
            store_id 店铺id
    */
    public function getChannelProductPrice($channel_id = 0, $store_id = 0)
    {
        $Dao = M("kfsy_channel_store.kf_channel_product");

        $where = array();
        $where[] = sprintf("fk_channel_id = %s", $channel_id);
        $where[] = sprintf("fk_store_id = %s", $store_id);

        $where = implode(" AND ", $where);

        $stockInfo = $Dao
            ->field("other_product_id, product_price")
            ->where($where)
            ->select();

        return $stockInfo;
    }

}
?>